package client.ui;

import java.util.List;

import phasebook.client.msgs.UserInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;

public class UserListSection extends SectionStackSection implements ResponseHandler{
	public enum Mode{Friends,Requests,MyRequests}
	
	private final Mode mode;
	private final Controller controller;
	
	private VLayout layout = new VLayout();
	public UserListSection(Controller controller,Mode mode){
		super(title(mode));
		this.mode = mode;
		this.controller = controller;
		this.controller.addResponseHandler(this);
		super.setExpanded(true);
		super.setResizeable(true);

		layout.setHeight("25%");
		layout.setOverflow(Overflow.AUTO);
		super.addItem(layout);
	}
	private static String title(Mode mode){
		switch(mode){
			case Friends: return "My Friends";
			case Requests: return "Friend Requests";
			case MyRequests: return "My Requests";
			default: return null;
		}
	}
	
	private class FriendTStrip extends ToolStrip implements ClickHandler{
		private UserInfo user;
		private ToolStripButton actionButton;
		private ToolStripButton personButton;
		private FriendTStrip(UserInfo user){
			this.user = user;
			super.setWidth100();
			super.setHeight(25);
			
			personButton = new ToolStripButton(user.getName());
			personButton.setIcon("[SKIN]/headerIcons/person.png");
			personButton.setTooltip("Open Profile");
			personButton.setWidth100();
			personButton.setAlign(Alignment.LEFT);
			super.addButton(personButton);
			personButton.addClickHandler(this);
			
			actionButton = new ToolStripButton("");
			switch(mode){
				case Friends:
					actionButton.setIcon("[SKIN]/headerIcons/minus.png");
					actionButton.setTooltip("Remove Friendship");
					break;
				case Requests:
					actionButton.setIcon("[SKIN]/headerIcons/plus.png");
					actionButton.setTooltip("Accept Friendship");
					break;
				case MyRequests:
					actionButton.setIcon("[SKIN]/headerIcons/minus.png");
					actionButton.setTooltip("Cancel Friend Request");
					break;
			}
			super.addButton(actionButton);
			actionButton.addClickHandler(this);
		}
		@Override
		public void onClick(ClickEvent event) {
			if(event.getSource()==actionButton){
				switch(mode){
					case Friends:
						controller.cancelFriendship(user.getUserId());
						break;
					case Requests:
						controller.acceptFriendshipRequest(user.getUserId());
						break;
					case MyRequests:
						controller.cancelFriendshipRequest(user.getUserId());
						break;
				}
			} else if (event.getSource()==personButton){
				controller.showUser(user.getUserId());
			}
		}
	}

	
	public void setFriends(List<UserInfo> friends){
		if (friends!=null){
			FriendTStrip[] members = new FriendTStrip[friends.size()];
			
			for(int i=0;i<friends.size();i++)
				members[i] = new FriendTStrip(friends.get(i));
			layout.setMembers(members);
		}
	}
	@Override
	public void onResponse(CombinedResponse response) {
		switch(mode){
			case Friends:
				setFriends(response.getFriends());
				break;
			case Requests:
				setFriends(response.getFriendRequests());
				break;
			case MyRequests:
				setFriends(response.getMyFriendRequests());
				break;
		}
	}
}
